import 'package:shared_preferences/shared_preferences.dart';

/// Create： Zhang 3/30/21 10:32 AM
/// 描述：本地数据存储
class SPUtil {
  ///存储字符串
  static Future putString(String key, String? value) async {
    if (value == null) return;

    final SharedPreferences sp = await SharedPreferences.getInstance();
    await sp.setString(key, value);
  }

  ///取出字符串
  static Future<String> getString(String key) {
    return SharedPreferences.getInstance().then(
      (sp) {
        return (sp.getString(key) ?? "");
      },
    );
  }

  ///存储整形数据
  static Future putInt({required String key, required int value}) async {
    final SharedPreferences sp = await SharedPreferences.getInstance();
    await sp.setInt(key, value);
  }

  static Future<int?> getInt(String key) {
    return SharedPreferences.getInstance().then(
      (sp) {
        return (sp.getInt(key));
      },
    );
  }

  ///存储布尔类型的值
  static void putBool(String key, bool value) async {
    final SharedPreferences sp = await SharedPreferences.getInstance();
    await sp.setBool(key, value);
  }

  ///获取布尔类型的值
  static Future<bool> getBool(String key) {
    return SharedPreferences.getInstance().then(
      (sp) {
        return (sp.getBool(key) ?? false);
      },
    );
  }
}
